package com.lsl.builder_pattern.practice.practice2;


import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * ConcreteBuilder（具体的建造者）
 * 负责实现Builder角色接口的类，这里定义了在生成实例的时候实际被调用的方法
 *
 * @author Zed
 * @description
 * @date 2022/11/27 13:35
 */
public class HTMLBuilder extends Builder {
    private String fileName;
    private PrintWriter printWriter;


    @Override
    protected void buildTitle(String title) {
        fileName = title + ".html";
        try {
            printWriter = new PrintWriter(new FileWriter(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        printWriter.println("<html><head><title>" + title + "</title></head><body>");
        //输出标题
        printWriter.println("<h1>" + title + "</h1>");
    }

    @Override
    protected void buildString(String str) {
        printWriter.println("<p>" + str + "</p>");
    }

    @Override
    protected void buildItems(String[] items) {
        printWriter.println("<ul>");
        for (int i = 0; i < items.length; i++) {
            printWriter.println("<li>" + items[i] + "</li>");
        }
        printWriter.println("</ul>");
    }

    @Override
    protected void buildClose() {
        printWriter.println("</body></html>");
        printWriter.close();
    }

    public String getResult() {
        return fileName;
    }
}
